/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sgs.dao;

import br.com.sgs.entity.Etapa;
import java.util.List;
import javax.persistence.Query;

/**
 *
 * @author Eric
 */
public class DAOEtapa extends AbstractDAO implements IDAOEtapa{
    
    public DAOEtapa(){
        super();
    }
    
    @Override
    public Etapa findEtapaById(int idetapa) {
        try {

            Query q = em.createQuery("SELECT E FROM Etapa e WHERE e.id = :idetapa");
            q.setParameter("idetapa", idetapa);
            return (Etapa) q.getSingleResult();

        } catch (Exception e) {
            System.err.println("Erro no findEtapaById");
            return null;
        }
    }

    @Override
    public List<Etapa> findEtapa() {
        try {

            Query q = em.createQuery("SELECT e FROM Etapa e");
            return q.getResultList();

        } catch (Exception e) {
            System.err.println("Erro no findEtapa");
            e.printStackTrace();
            return null;
        }
    }
    
    @Override
    public List<Etapa> findEtapaByDescricao(String descricao) {
        try {

            Query q = em.createQuery("SELECT e FROM Etapa e WHERE UPPER(e.descricao) LIKE :descricao");
            q.setParameter("descricao", "%"+descricao+"%");
            return q.getResultList();

        } catch (Exception e) {
            System.err.println("Erro no findEtapaByDescricao");
            e.printStackTrace();
            return null;
        }
    }
    
    public Etapa findEtapaByName(String descricao) {
        try {
            Query q = em.createQuery("SELECT e FROM Etapa e WHERE UPPER(e.descricao) LIKE :descricao");
            q.setParameter("descricao", descricao);
            return (Etapa) q.getSingleResult();
        } catch (Exception erro) {
            System.err.println("Erro na consulta findEtapaByName");
            erro.printStackTrace();
            return null;
        }
    }
}   
